using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Qreed.Windows.Forms;
using System.Diagnostics;
using System.Threading;
using System.IO;

namespace ProgressDialogDemo
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();

            folderTreeView1.PopulateTree();
            folderTreeView1.SelectedPath = @"c:\windows";
        }

        private void button1_Click(object sender, EventArgs e)
        {
            ProgressDialog dlg = new ProgressDialog();

            // Note that I call a static function (Worker.DoWork) as work around for the "func eval" problem.
            // Problem only occurs when debugging.
            // See http://blogs.msdn.com/greggm/archive/2005/11/18/494648.aspx

            dlg.Text = "Working on it ...";
            dlg.DoWork += new DoWorkEventHandler(Worker.DoWork);
            dlg.Argument = folderTreeView1.SelectedPath;
            
            DialogResult res = dlg.ShowDialog(this);

            if (res == DialogResult.OK)
            {
                MessageBox.Show("Total of " + dlg.Result.Value.ToString() + " items processed.");
            }
            else
            {
                if (dlg.Result.IsCancelled)
                    MessageBox.Show("Cancelled.");
                else
                    MessageBox.Show(dlg.Result.Exception.ToString());
            }
        }

        private class Worker
        {
            public static void DoWork(object sender, DoWorkEventArgs e)
            {
                ProgressDialog dlg = (ProgressDialog)sender;

                int processed = 0;

                string folder = (string)e.Argument;
                string[] files = Directory.GetFiles(folder);

                if (files != null && files.Length > 0)
                {
                    double percent = 0.0;
                    double step = 100.0 / files.Length;

                    for (int i = 0; i < files.Length; i++)
                    {
                        string file = files[i];

                        percent = (step * (i+1)) + 0.5;

                        dlg.ReportProgress((int)percent, "Processing \"" + Path.GetFileName(file) + "\" ... this text is waaaaaaaaaaaaaaaaaaaaaaaay too long");

                        if (dlg.CancellationPending)
                        {
                            e.Cancel = true;
                            break;
                        }

                        Thread.Sleep(200);
                        
                        processed += 1;
                    }
                }

                if (!e.Cancel)
                    dlg.ReportProgress(100, "Done");
                else
                    dlg.AutoClose = true;

                e.Result = processed;
            }
        }
    }
}